package com.cdshangpai.photoapp.ui.main;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.cdshangpai.PhotoappApplication;
import com.cdshangpai.photoapp.MainActivity;
import com.cdshangpai.photoapp.databinding.LoginFragmentBinding;
import com.cdshangpai.utils.db.AccountDb;
import com.cdshangpai.utils.db.beans.Account;

import java.util.List;

public class LoginFragment extends Fragment {
    AccountDb accountDb;
    LoginFragmentBinding binding;
    public String errormsg="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        accountDb=new AccountDb();

        binding= LoginFragmentBinding.inflate(inflater);
        binding.setView(this);


        View view=binding.getRoot();
        return view;
    }


    public void onLogin()
    {
        String account=binding.loginName.getText().toString();
        String password=binding.loginPassword.getText().toString();
        if(TextUtils.isEmpty(account)||TextUtils.isEmpty(password))
        {
            errormsg="请填写账号和密码后，登录系统。";
            binding.setView(this);
            return;
        }

        List<Account> accounts=accountDb.query(account,password);
        if(accounts.size()<1)
        {
            errormsg="账号或密码错误，请修改后登录。";
            binding.setView(this);
            return;
        }

        Account dbaccount=accounts.get(0);
        saveCacheAccount(JSONObject.toJSONString(dbaccount));

        //成功登录，清除输入框内容
        binding.loginName.setText("");
        binding.loginPassword.setText("");

        //登录完成，跳转到主页显示
        MainActivity mainActivity=(MainActivity)getActivity();
        mainActivity.switchFragment();
    }

    /**
     * 退出系统登录
     */
    public static void logout()
    {
        cleanCacheAccount();
    }

    /**
     * 检查是否登录
     * @return
     */
    public static Boolean logined()
    {
        String buf=loadCacheAccount();
        return !TextUtils.isEmpty(buf);
    }

    /**
     * 本地存储获取账号
     * @return
     */
    public static Account pickCacheAccount()
    {
        String buf=loadCacheAccount();
        if(TextUtils.isEmpty(buf))
        {
            return null;
        }
        return JSONObject.parseObject(buf,Account.class);
    }

    private static String cacheAccountKey="chcheaccount";

    private static SharedPreferences getStorage()
    {
        return PhotoappApplication.context.getSharedPreferences(cacheAccountKey, Context.MODE_PRIVATE);
    }

    private static String cacheAccountInfo="";

    private static String loadCacheAccount(){
//        SharedPreferences refs= getStorage();
//        String account=refs.getString(cacheAccountKey,"");
        String account=cacheAccountInfo;
        return account;
    }

    private static void saveCacheAccount(String account){
        SharedPreferences sp= getStorage();
        sp.edit().putString(cacheAccountKey,account).commit();
        cacheAccountInfo=account;
    }

    private static void cleanCacheAccount()
    {
        SharedPreferences sp= getStorage();
        sp.edit().remove(cacheAccountKey).commit();
        cacheAccountInfo="";
    }

    public void gotoLog()
    {
        Intent intent=new Intent(getActivity(),LogActivity.class);
        getActivity().startActivity(intent);
    }
}
